package com.ktdaddy.config.shiro.realm;

import com.ktdaddy.pojo.Kernel;
import com.ktdaddy.service.PermService;
import com.ktdaddy.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author kdaddy@163.com
 * @date 2021/4/27 23:35
 */
@Component
public class RealmHelper {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermService permService;

    public AuthorizationInfo buildCommonInfo(){
        // 获取当前用户
        Kernel currentUser = (Kernel) SecurityUtils.getSubject().getPrincipal();
        // UserEntity currentUser = (Kernel)principals.getPrimaryPrincipal();
        // 查询数据库，获取用户的角色信息
        Set<String> roles = roleService.loadByKernelName(currentUser.getKernelName());
        // 查询数据库，获取用户的权限信息
        Set<String> perms = permService.loadByKernelName(currentUser.getKernelName());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(roles);
        info.setStringPermissions(perms);
        return info;
    }
}
